package vn.edu.saigontech.ncms.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.info.ReceiptRefund;
import vn.edu.saigontech.ncms.service.ReceiptRefundService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class ReceiptRefundServiceImpl extends HibernateGenericService<ReceiptRefund, Long> implements ReceiptRefundService{

	@Override
	public ReceiptRefund findByReceiptId(Long receiptId) {
		Criteria criteria = this.createCriteria();
		criteria.createCriteria("receipt").add(Restrictions.eq("id", receiptId));
		return (ReceiptRefund) criteria.uniqueResult();
	}

	@Override
	public List<ReceiptRefund> findByDate(Date fromDate, Date toDate) {
		Criteria criteria = this.createCriteria();
		if(!Checker.isNull(fromDate)) {
			criteria.add(Restrictions.ge("date", fromDate));
		}
		if(!Checker.isNull(toDate)) {
			criteria.add(Restrictions.le("date", toDate));
		}
		criteria.addOrder(Order.asc("date"));
		return criteria.list();
	}
}